package def;


import java.text.ParseException;

import Lights.DirectedLight;
import Lights.Light;
import Lights.PointLight;
import PysicalSurfacesObjects.Box;
import PysicalSurfacesObjects.Cylinder;
import PysicalSurfacesObjects.Disc;
import PysicalSurfacesObjects.Entity;
import PysicalSurfacesObjects.Rectangle;
import PysicalSurfacesObjects.Sphere;
import PysicalSurfacesObjects.Cylinder;
//import Parser.ParseException;


public class ourParser extends Parser{
	
	private Scene scene;
	private IAllSceneObjects curObject;
	public ourParser(Scene scene) {
		this.scene = scene;
	}
	
	///////////////////// override these methods in your implementation //////////////////
	
	public void startFile()
	{
		System.out.println("----------------");
	}

	public void endFile() throws ParseException
	{
		System.out.println("================");
	}

	// start a new object definition
	// return true if recognized
	public boolean addObject(String name) throws ParseException
	{
		System.out.println("OBJECT: " + name);
		switch (name.toLowerCase())
		{
		case "scene":
			curObject = null;
			break;
		case "camera":
			curObject = new Camera();
			scene.setCamera((Camera)curObject);
			break;
		case "light-point":
			curObject = new PointLight();
			scene.addLight((Light)curObject);
			break;
		case "light-directed":
			curObject = new DirectedLight();
			scene.addLight((Light)curObject);
			break;
		case "rectangle":
			curObject = new Rectangle();
			scene.addEntity((Entity)curObject);
			break;
		case "disc":
			curObject = new Disc();
			scene.addEntity((Entity)curObject);
			break;
		case "sphere":
			curObject = new Sphere();
			scene.addEntity((Entity)curObject);
			break;
		case "box":
			curObject = new Box();
			scene.addEntity((Entity)curObject);
			break;
		case "cylinder":
			curObject = new Cylinder();
			scene.addEntity((Entity)curObject);
			break;		
		default:
			return false;
		}
		return true;
	}
	
	// set a specific parameter for the current object
	// return true if recognized
	public boolean setParameter(String name, String[] args) throws ParseException 
	{
		System.out.print("PARAM: " + name);
	    for (String s : args) 
	        System.out.print(", " + s);
	    System.out.println();
	    
	    if(curObject == null)
	    	scene.setParameter(name, args);
	    else
	    {
	    	curObject.setParameter(name, args);
	    	if(curObject instanceof Entity)
	    		scene.addEntity((Entity)curObject);
	    	else if(curObject instanceof Light)
		    	scene.addLight((Light)curObject);
	    }
	    
	    return true;
	}
	
	// finish the parsing of the current object
	// here is the place to perform any validations over the parameters and
	// final initializations.
	public void commit() throws ParseException
	{
		
	}
	
	public void reportError(String err)
	{
		System.out.println("ERROR: " + err);
	}
	
	
	///MORE UTILS
	public static double[] parseArrayOfDoubles(String[] arrayOfString) {

		int len = arrayOfString.length;
		double[] ans = new double[len];
		for (int i=0; i<len; i++) {
			ans[i] = Double.parseDouble(arrayOfString[i]);
		}
		return ans;
		
	}
	
	
	
}
